using UnityEngine;

public struct MuscleTimeReflex
{
	public const int ImpulseMin = 150;

	public const int ImpulseMax = 155;

	public float timeReflex;

	public Rigidbody2D body;

	public Joint2D joint;

	private int timeStep;

	private float sizeTimeStep;

	private int currentTimeStep;

	public MuscleTimeReflex(Joint2D joint)
	{
		timeReflex = Random.Range(0.5f, 6f);
		this.joint = joint;
		body = joint.GetComponent<Rigidbody2D>();
		timeStep = 0;
		sizeTimeStep = Random.Range(0.2f, 0.5f);
		currentTimeStep = 0;
	}

	public bool UpdateReflex(float deltaTime)
	{
		if (timeReflex > 0f && joint != null && joint.enabled)
		{
			timeReflex -= deltaTime;
			currentTimeStep = Mathf.FloorToInt(timeReflex / sizeTimeStep);
			if (timeStep != currentTimeStep)
			{
				if (Random.value < 0.5f)
				{
					body.AddTorque((0f - body.mass) * (float)Random.Range(150, 155));
				}
				else
				{
					body.AddTorque(body.mass * (float)Random.Range(150, 155));
				}
				timeStep = currentTimeStep;
			}
			return true;
		}
		timeReflex = 0f;
		return false;
	}

	public void Destroy()
	{
		timeReflex = 0f;
		joint = null;
		body = null;
	}
}
